<?php
include_once('config/config.php');

$LoginAdmin = checkAuth();
// 当前页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 4;

// 分页开始的偏移值
$offset = ($page - 1) * $limit;

// 条件变量
$where = '1';

// 获取总条数
// $countSql = "SELECT COUNT(*) AS count FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` WHERE $where";
$countSql = "SELECT COUNT(*) AS count FROM `pre_department` WHERE $where";
// 获取数据
$depSql = " SELECT * FROM `pre_department` ORDER BY `id` ASC LIMIT $offset,$limit ";

$depList = all($depSql);
// var_dump($depList);
// foreach($depList as $q){
//     echo $q['id'];
// }
// print_r($depList['id']);
// exit;

$count = find($countSql);

$showPage = showPage($page, $count['count'], $limit);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>

<body>

    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">部门列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">depList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" id="add"><i class="icon-plus"></i>添加部门</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>部门</th>
                                <th>岗位</th>
                                <th style="width: 30px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($depList as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td>
                                        <?php 
                                            $countSql = "SELECT COUNT(*) AS count FROM `pre_job` WHERE `depid` = {$item['id']}";
                                            $count = find($countSql);
                                            echo $count['count'];
                                        ?>
                                    </td>
                                    <td>
                                        <a href="depEdit.php?id=<?php echo $item['id'];?>" class="edit"><i class="icon-pencil"></i></a>
                                        <!-- 点击后出现确认框是否 -->

                                        <a href="depDel.php?id=<?php echo $item['id'];?>" class="del"><i class="icon-remove" ></i></a>
                                    </td>
                                </tr>
                            <?php } ?>


                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                   <?php echo $showPage; ?>
                </div>



                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>


<?php include_once('common/script.php'); ?>
<script>
    // 添加弹窗
    $('#add').click(function(){
        
        $dep = prompt("请输入部门名");
        if ($dep) {
            location.href = 'depAdd.php' + '?dep=' + $dep;
            return true;
        }
    });

    // 编辑弹窗
    $('.edit').click(function(e) {
        e.preventDefault();
        $dep = prompt("请输入更新后的部门名");
        if ($dep) {
            location.href = $(this).attr('href') + '&dep=' + $dep;
            return true;
        } else {
            return false;
        }
    });

    // 删除弹窗
    $('.del').click(function(e) {
       e.preventDefault();
        if (confirm('是否删除')) {
            location.href = $(this).attr('href');
            return true;
        } else {

            return false;
        }
    });
</script>